package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.flag.ResourcesPath;
	import engine.net.datatype.WString;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.util.FilterUtil;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.comboBox.NxComboBoxEvent;
	import newx.com.control.window.NxWindowEvent;

	public class FactionBattleGiftLogic
	{
		private static var _self					:FactionBattleGiftLogic
		private var 	_ui							:FactionBattleGiftUI;
		private var 	_model						:ModelLocator;
		private var   _factionUser					:Array =[];
		
		private var  _currCombos1					:String="";
		private var  _currCombos2					:String="";
		private var  _currCombos3					:String="";
		
		private var _bindMang:BindManage;
		private var _bindIndex:int=-1;
		public static function getInstance():FactionBattleGiftLogic
		{
			if(_self == null)
			{
				_self = new FactionBattleGiftLogic();
			}
			return _self;
		}
		public function FactionBattleGiftLogic()
		{
		}
		public function  Init():void
		{
			_bindMang = BindManage.getInstance();
			_ui 	= FactionBattleGiftUI.getInstance();
			_model	= ModelLocator.getInstance();
			//获取是否有帮派战礼包
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_NOT_GET_GUILDFIGHT_REWARD),getIsHaveBox);
			//获取帮派成员列表
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_GUILDFIGHT_CAN_AWARD_PLAYER),getFactionMsg);
			_ui.combos1.defaultTextColor=_ui.combos2.defaultTextColor=_ui.combos3.defaultTextColor =0xffa800;
		}
//		
//		public function getCommboxData():void
//		{
//			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_GET_GUILD_NAME);
//		}
		
		public function	BeforeShow():void
		{
			_ui.mc2.source=_ui.mc3.source=_ui.mc4.source = ResManager.newResClass(ResourcesPath.getPropsURL(45002005));
			bind();
//			updateState();
			addEvent();
			_ui.infolabel.htmlText = StaticIni.getSwfLangStr("UIString_1000507");
		}
		private function bind():void
		{
			if(_bindIndex ==-1)
			{
				_bindIndex =_bindMang.BindProp(_model.scene.Role,"WinMaster",this,"BoxLv");
			}
		}
		private function unbind():void
		{
			if(_bindIndex !=-1)
			{
				_bindIndex =_bindMang.UnbindProp(_model.scene.Role,"WinMaster",_bindIndex);
			}
		}
		public function set BoxLv(val:int):void
		{
			var lv:int =val;
			var model:int =45002005;
			if(lv == 1)
			{
				model =45002005;
				_ui.label1.htmlText =_ui.label2.htmlText =_ui.label3.htmlText =StaticIni.getSwfLangStr("UIStr_1001901");
			}
			else
			{
				model =45002004;
				_ui.label1.htmlText =_ui.label2.htmlText =_ui.label3.htmlText =StaticIni.getSwfLangStr("UIStr_1001902");
			}
			_ui.mc2.source=_ui.mc3.source=_ui.mc4.source = ResourcesPath.getPropsURL(model);
		}
		public function	AfterHide():void
		{
			removeEvent();
			unbind();
			_ui.combos1.dataProvider=[]		
			_ui.combos2.dataProvider=[]		
			_ui.combos3.dataProvider=[]		

		}
		private function addEvent():void
		{
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,	onCliose);
			_ui.combos1.addEventListener(MouseEvent.CLICK,onClick,true);
			_ui.combos2.addEventListener(MouseEvent.CLICK,onClick,true);
			_ui.combos3.addEventListener(MouseEvent.CLICK,onClick,true);
			_ui.combos1.addEventListener(NxComboBoxEvent.NXCOMBOBOX_CLICK, onClickComBoboxHandle);
			_ui.combos2.addEventListener(NxComboBoxEvent.NXCOMBOBOX_CLICK, onClickComBoboxHandle);
			_ui.combos3.addEventListener(NxComboBoxEvent.NXCOMBOBOX_CLICK, onClickComBoboxHandle);
			_ui.assign.addEventListener(MouseEvent.CLICK,					onClikc);
			
		}
		private function removeEvent():void
		{
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,	onCliose);
			_ui.combos1.removeEventListener(MouseEvent.CLICK,onClick,true);
			_ui.combos2.removeEventListener(MouseEvent.CLICK,onClick,true);
			_ui.combos3.removeEventListener(MouseEvent.CLICK,onClick,true);
			_ui.combos1.removeEventListener(NxComboBoxEvent.NXCOMBOBOX_CLICK, onClickComBoboxHandle);
			_ui.combos2.removeEventListener(NxComboBoxEvent.NXCOMBOBOX_CLICK, onClickComBoboxHandle);
			_ui.combos3.removeEventListener(NxComboBoxEvent.NXCOMBOBOX_CLICK, onClickComBoboxHandle);
			_ui.assign.removeEventListener(MouseEvent.CLICK,					onClikc);
		}
		private function onCliose(e:NxWindowEvent):void
		{
			_ui.hide();
		}
		private function onClick(e:MouseEvent):void
		{

			switch(e.currentTarget)
			{
				case _ui.combos1:
					_ui.combos1.dataProvider = UpdateFactionUser(_factionUser,_ui.combos1);
					var index:int = getPos(_currCombos1,_ui.combos1.dataProvider);
					if(index !=-1)
						_ui.combos1.setSelected(index)
					break;
				case _ui.combos2:
					_ui.combos2.dataProvider = UpdateFactionUser(_factionUser,_ui.combos2);
//					_ui.combos2.setSelected(_factionUser.indexOf(_setdata));
					var index1:int = getPos(_currCombos2,_ui.combos2.dataProvider);
					if(index1 !=-1)
						_ui.combos2.setSelected(index1)
					break;
				case _ui.combos3:
					_ui.combos3.dataProvider = UpdateFactionUser(_factionUser,_ui.combos3);
//					_ui.combos3.setSelected(_factionUser.indexOf(_setdata));
					var index2:int = getPos(_currCombos3,_ui.combos3.dataProvider);
					if(index2 !=-1)
						_ui.combos3.setSelected(index2)
					break;
			}
			updateState();
		}
		private function getIsHaveBox(e:CustomMsgEvent):void
		{
			var state:int 	= e.pList[0];
			var lv	:int	= e.pList[1];
			if(state == 1)
			{
//				RightTopIndexUI.getInstance().btn11.visible=true;
			}
			else
			{
//				RightTopIndexUI.getInstance().btn11.visible=false;
			}
		}
		private function updateState():void
		{
			if(_ui.combos1.getSelecedItem() ==null)
			{
				_ui.state1.htmlText = StaticIni.getSwfLangStr("GuildUIStr_46");
			}
			else
			{
				_ui.state1.htmlText = StaticIni.getSwfLangStr("GuildUIStr_45");
			}
			if(_ui.combos2.getSelecedItem() ==null)
			{
				_ui.state2.htmlText = StaticIni.getSwfLangStr("GuildUIStr_46");
			}
			else
			{
				_ui.state2.htmlText = StaticIni.getSwfLangStr("GuildUIStr_45");
			}
			if(_ui.combos3.getSelecedItem() ==null)
			{
				_ui.state3.htmlText = StaticIni.getSwfLangStr("GuildUIStr_46");
			}
			else
			{
				_ui.state3.htmlText = StaticIni.getSwfLangStr("GuildUIStr_45");
			}
			
		}
		private function onClikc(e:MouseEvent):void
		{
//			_ui.hide();
			
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_GUILDFIGHT_REWARD,new WString(_currCombos1),new WString(_currCombos2),new WString(_currCombos3));
		}
		private var _setdata:Object;
		private function onClickComBoboxHandle(e:NxComboBoxEvent):void
		{
			switch(e.target)
			{
				case _ui.combos1:
					_currCombos1 = e.item.data;
			
					break;
				case _ui.combos2:
					_currCombos2 = e.item.data;
					break;
				case _ui.combos3:
					_currCombos3 = e.item.data;
					break;
			}
			_setdata=e.item;

		}
		private function getPos(str:String,arr:Array):int
		{
			for(var i:String  in  arr)
			{
				if(arr[int(i)].label == str)
				{
					return int(i);
				}
			}
			return -1;
		}
		private function clearUI():void
		{
			_ui.combos1.dataProvider=[]		
			_ui.combos2.dataProvider=[]		
			_ui.combos3.dataProvider=[]		
			_factionUser=[];
			_currCombos1 = _currCombos2 =_currCombos3 =""
			_ui.state1.text = StaticIni.getSwfLangStr("GuildUIStr_46")
			_ui.state2.text = StaticIni.getSwfLangStr("GuildUIStr_46");
			_ui.state3.text = StaticIni.getSwfLangStr("GuildUIStr_46");
		}
		private function getFactionMsg(e:CustomMsgEvent=null):void
		{
			clearUI();
			var index:int;
			var infoArr:Array =e.pList;
			for(var i:int=0;i<infoArr.length;i++)
			{
				index = _factionUser.indexOf(infoArr[i]);
				if(index == -1)
				{
					_factionUser.push({label:infoArr[i],data:infoArr[i]});
				}
				
			}
			switch(infoArr.length)
			{
				case 0:
					return;
				case 1:
					_ui.combos1.dataProvider=_factionUser;
					_currCombos1=_ui.combos1.title;
					_ui.combos2.mouseEnabled =_ui.combos3 .mouseEnabled=false;
					_ui.combos2.mouseChildren =_ui.combos3 .mouseChildren=false;
					_ui.combos3.filters =[FilterUtil.GrayColorFilter];
					_ui.combos2.filters =[FilterUtil.GrayColorFilter]
					break;
				case 2:
					_ui.combos1.dataProvider=_factionUser;
					_ui.combos2.dataProvider=_factionUser;
					_ui.combos1.setSelected(0);
					_ui.combos2.setSelected(1);
					_currCombos1=_ui.combos1.title;
					_currCombos2=_ui.combos2.title;
					_ui.combos3 .mouseChildren=false;
					_ui.combos3.mouseEnabled ==false;
					_ui.combos3.filters =[FilterUtil.GrayColorFilter]

					break;
				default:
					_ui.combos1.dataProvider=_factionUser;
					_ui.combos2.setSelected(0);
					_ui.combos2.dataProvider=_factionUser;
					_ui.combos2.setSelected(1);
					_ui.combos3.dataProvider=_factionUser;
					_ui.combos3.setSelected(2);
					_currCombos1=_ui.combos1.title;
					_currCombos2=_ui.combos2.title;
					_currCombos3=_ui.combos3.title;
					_ui.combos1.mouseChildren= _ui.combos2.mouseChildren =_ui.combos3 .mouseChildren=true;
					_ui.combos1.mouseEnabled=_ui.combos2.mouseEnabled =_ui.combos3 .mouseEnabled=true;
					_ui.combos3.filters =null;
					_ui.combos3.filters =null
			}
			if(_ui.created == false)
			{
				_ui.showOrHide();
			}
//			
		}
		private function getIndex(str:String,arr:Array):int
		{
			for(var i:String in arr)
			{
				if(arr[int(i)].label ==str)
				{
					return int(i);
				}
			}
			return -1;
		}
		/**
		 *根据选择的从数组中删除一个成员 
		 * @param name
		 * 
		 */		
		private function UpdateFactionUser(arr:Array,mc:DisplayObject):Array
		{
			var arr1:Array =_factionUser.concat();
			if(_ui.combos1.data && _ui.combos1 !=mc)
			{
				if(this._currCombos1!= "")
				{
					
					var index:int =getIndex(_currCombos1,arr1);
					if(index !=-1)
					{
						arr1.splice(index,1);
					}
				}
				
			}
			if(_ui.combos2.data&& _ui.combos2 !=mc)
			{
				if(_currCombos2 != "")
				{
					var index1:int = getIndex(_currCombos2,arr1);
					if(index1 !=-1)
					{
						arr1.splice(index1,1);
					}
				}
				
			}
			if(_ui.combos3.data && _ui.combos3 !=mc)
			{
				if(_currCombos3 != "")
				{
					var index2:int = getIndex(_currCombos3,arr1);;
					if(index2 !=-1)
					{
						arr1.splice(index2,1);
					}
				}
				
			}
			return arr1;
			
		}
	}
}